<?php defined ( 'SYSPATH' ) or die ( 'No direct access allowed.' );

/**
 * Class Controller_Saleorders
 * 追踪号
 *
 *
 */
class Controller_Report_Saleorders extends Controller_Admin
{
    public function before()
    {
        try {
            ACL::Required('saleorders report');
        } catch ( HTTP_Exception_403 $e ) {
            Message::error('sorry，你没有权限进这个目录');
            $this->request->redirect ( URL::base (true) );
        }
        parent::before();
    }
    public function action_index(){
        $this->title=__("订单追踪号查询功能");
        $view = View::factory('report/saleorders');
        $this->response->body($view);
    }
    public function action_select(){
        $this->title=__("订单追踪号查询功能");
        $starttime = isset($_GET['date_from'])?$_GET['date_from']:'';
        $endtime = isset($_GET['date_to'])?$_GET['date_to']:'';
        $mailcode = isset($_GET['mailcode'])?$_GET['mailcode']:'';

        $sql = "SELECT count(0) as num FROM `erp_orders` WHERE 1 ";
        if($starttime!=''){
            $sql .=" AND orders_out_time>'".$starttime." 00:00:00'";
        }
        if($endtime!=''){
            $sql .=" AND orders_out_time<='".$endtime." 59:59:59'";
        }
        if($mailcode!=''){
            $sql .=" AND (orders_mail_code='".$mailcode."' OR orders_shipping_code='".$mailcode."')";
        }

        $ordernum = DB::query(Database::SELECT,$sql)->execute('readonlyerp')->as_array();
        $per_page = 1000;
        $pagination = Pagination::factory(array(
            'items_per_page' => $per_page,
            'total_items'    => $ordernum[0]['num'],
            'uri'	         => $this->request->uri(),
        ));

        $sql = "SELECT `erp_orders_id`,`ebay_orders_id`,`buyer_id`,`buyer_email`,`buyer_name`,`buyer_country`,
        `ebay_counycode`,`orders_total`,`currency_type`,`currency_value`,`sales_account`,`orders_type`,`orders_status`,
        `heavi`,`fee`,`orders_out_time`,`orders_mail_code`,`orders_shipping_code`,`erp_post_office` FROM `erp_orders` WHERE 1 ";
        if($starttime!=''){
            $sql .=" AND orders_out_time>='".$starttime." 00:00:00'";
        }
        if($endtime!=''){
            $sql .=" AND orders_out_time<='".$endtime." 59:59:59'";
        }
        if($mailcode!=''){
            $sql .=" AND (orders_mail_code='".$mailcode."' OR orders_shipping_code='".$mailcode."')";
        }
        $current_page     = isset($_GET['page'])?$_GET['page']:1;
        $sql      .=" order by orders_out_time DESC LIMIT ".(($current_page-1)*$per_page).",".$per_page;
        $orders = DB::query(Database::SELECT,$sql)->execute('readonlyerp')->as_array();

        //做数组，sql语句变成in查询
        $country_cnname = $serivename = $postname = '';
        foreach($orders as $_order){
           if(!empty($_order['buyer_country'])) $country_cnname .="'".trim(addslashes($_order['buyer_country']))."',";
           if(!empty($_order['sales_account'])) $serivename .="'".trim($_order['sales_account'])."',";
           if(!empty($_order['erp_post_office'])) $postname .="'".trim($_order['erp_post_office'])."',";
        }

        //查询国家中文名
        if(!empty($country_cnname))$country_cnname_arr = saleorders::getcountrycnname(substr($country_cnname,0,-1));
        //通过账号查询客服名字
        if(!empty($serivename))$serivename_arr = saleorders::getsalename(substr($serivename,0,-1));
        //查询物流渠道中文名称
        if(!empty($postname))$postname_arr = saleorders::getshipname(substr($postname,0,-1));

        $ordertypes = saleorders::getordertype();//查询订单类型
        $orderstatus = saleorders::getorderstatus();//查询订单状态
        foreach($orders as $key=>$order){
            $orders[$key]['ordertotal'] = $order['currency_type'].' '.$order['orders_total'];
            $orders[$key]['order_type_name'] = isset($ordertypes[$order['orders_type']])?$ordertypes[$order['orders_type']]:'无';
            $orders[$key]['orders_status'] = isset($orderstatus[$order['orders_status']])?$orderstatus[$order['orders_status']]:'无';
            //$orders[$key]['country_cnname'] = saleorders::getcountrycnname(trim($order['buyer_country']));
            //$orders[$key]['serivename'] = saleorders::getsalename(trim($order['sales_account']));
            //$orders[$key]['postname'] = saleorders::getshipname($order['erp_post_office']);
            $orders[$key]['country_cnname'] = isset($country_cnname_arr[trim($order['buyer_country'])])?$country_cnname_arr[trim($order['buyer_country'])]:'无';
            $orders[$key]['serivename'] = isset($serivename_arr[trim($order['sales_account'])])?$serivename_arr[trim($order['sales_account'])]:'无';
            $orders[$key]['postname'] = isset($postname_arr[trim($order['erp_post_office'])])?$postname_arr[trim($order['erp_post_office'])]:'无';

        }

        $view = View::factory('report/saleorders')
                ->set('orders',$orders)
                ->set('starttime',$starttime)
                ->set('endtime',$endtime)
                ->set('mailcode',$mailcode)
                ->set('pagination',$pagination);
        $this->response->body($view);
    }
    public function action_down(){
        $this->title=__("订单追踪号查询功能");
        $view = View::factory('report/saleorders');
        $this->response->body($view);
    }

    public function action_gettype(){
        $ordertypes = saleorders::getorderstatus();
        print_r($ordertypes);
    }
}


